using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Commerce.Common
{
	/// <summary>
	/// Strongly-typed collection for the PromoBundle class.
	/// </summary>
	[Serializable]
	public partial class PromoBundleCollection : ActiveList<PromoBundle,PromoBundleCollection> 
	{	   
		public PromoBundleCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the CSK_Promo_Bundle table.
	/// </summary>
	[Serializable]
	public partial class PromoBundle : ActiveRecord<PromoBundle>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public PromoBundle()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public PromoBundle(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

        
		public PromoBundle(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public PromoBundle(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }

		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("CSK_Promo_Bundle", TableType.Table, DataService.GetInstance("dashCommerce"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarBundleID = new TableSchema.TableColumn(schema);
				colvarBundleID.ColumnName = "bundleID";
				colvarBundleID.DataType = DbType.Int32;
				colvarBundleID.MaxLength = 0;
				colvarBundleID.AutoIncrement = true;
				colvarBundleID.IsNullable = false;
				colvarBundleID.IsPrimaryKey = true;
				colvarBundleID.IsForeignKey = false;
				colvarBundleID.IsReadOnly = false;
				colvarBundleID.DefaultSetting = @"";
				colvarBundleID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarBundleID);
				
				TableSchema.TableColumn colvarBundleName = new TableSchema.TableColumn(schema);
				colvarBundleName.ColumnName = "bundleName";
				colvarBundleName.DataType = DbType.String;
				colvarBundleName.MaxLength = 50;
				colvarBundleName.AutoIncrement = false;
				colvarBundleName.IsNullable = false;
				colvarBundleName.IsPrimaryKey = false;
				colvarBundleName.IsForeignKey = false;
				colvarBundleName.IsReadOnly = false;
				
						colvarBundleName.DefaultSetting = @"((0))";
				colvarBundleName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarBundleName);
				
				TableSchema.TableColumn colvarDiscountPercent = new TableSchema.TableColumn(schema);
				colvarDiscountPercent.ColumnName = "discountPercent";
				colvarDiscountPercent.DataType = DbType.Int32;
				colvarDiscountPercent.MaxLength = 0;
				colvarDiscountPercent.AutoIncrement = false;
				colvarDiscountPercent.IsNullable = false;
				colvarDiscountPercent.IsPrimaryKey = false;
				colvarDiscountPercent.IsForeignKey = false;
				colvarDiscountPercent.IsReadOnly = false;
				
						colvarDiscountPercent.DefaultSetting = @"((0))";
				colvarDiscountPercent.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDiscountPercent);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "description";
				colvarDescription.DataType = DbType.String;
				colvarDescription.MaxLength = 500;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				TableSchema.TableColumn colvarCreatedOn = new TableSchema.TableColumn(schema);
				colvarCreatedOn.ColumnName = "createdOn";
				colvarCreatedOn.DataType = DbType.DateTime;
				colvarCreatedOn.MaxLength = 0;
				colvarCreatedOn.AutoIncrement = false;
				colvarCreatedOn.IsNullable = true;
				colvarCreatedOn.IsPrimaryKey = false;
				colvarCreatedOn.IsForeignKey = false;
				colvarCreatedOn.IsReadOnly = false;
				
						colvarCreatedOn.DefaultSetting = @"(getdate())";
				colvarCreatedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedOn);
				
				TableSchema.TableColumn colvarCreatedBy = new TableSchema.TableColumn(schema);
				colvarCreatedBy.ColumnName = "createdBy";
				colvarCreatedBy.DataType = DbType.String;
				colvarCreatedBy.MaxLength = 50;
				colvarCreatedBy.AutoIncrement = false;
				colvarCreatedBy.IsNullable = true;
				colvarCreatedBy.IsPrimaryKey = false;
				colvarCreatedBy.IsForeignKey = false;
				colvarCreatedBy.IsReadOnly = false;
				colvarCreatedBy.DefaultSetting = @"";
				colvarCreatedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCreatedBy);
				
				TableSchema.TableColumn colvarModifiedOn = new TableSchema.TableColumn(schema);
				colvarModifiedOn.ColumnName = "modifiedOn";
				colvarModifiedOn.DataType = DbType.DateTime;
				colvarModifiedOn.MaxLength = 0;
				colvarModifiedOn.AutoIncrement = false;
				colvarModifiedOn.IsNullable = true;
				colvarModifiedOn.IsPrimaryKey = false;
				colvarModifiedOn.IsForeignKey = false;
				colvarModifiedOn.IsReadOnly = false;
				
						colvarModifiedOn.DefaultSetting = @"(getdate())";
				colvarModifiedOn.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedOn);
				
				TableSchema.TableColumn colvarModifiedBy = new TableSchema.TableColumn(schema);
				colvarModifiedBy.ColumnName = "modifiedBy";
				colvarModifiedBy.DataType = DbType.String;
				colvarModifiedBy.MaxLength = 50;
				colvarModifiedBy.AutoIncrement = false;
				colvarModifiedBy.IsNullable = true;
				colvarModifiedBy.IsPrimaryKey = false;
				colvarModifiedBy.IsForeignKey = false;
				colvarModifiedBy.IsReadOnly = false;
				colvarModifiedBy.DefaultSetting = @"";
				colvarModifiedBy.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModifiedBy);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["dashCommerce"].AddSchema("CSK_Promo_Bundle",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("BundleID")]
		public int BundleID 
		{
			get { return GetColumnValue<int>(Columns.BundleID); }

			set { SetColumnValue(Columns.BundleID, value); }

		}

		  
		[XmlAttribute("BundleName")]
		public string BundleName 
		{
			get { return GetColumnValue<string>(Columns.BundleName); }

			set { SetColumnValue(Columns.BundleName, value); }

		}

		  
		[XmlAttribute("DiscountPercent")]
		public int DiscountPercent 
		{
			get { return GetColumnValue<int>(Columns.DiscountPercent); }

			set { SetColumnValue(Columns.DiscountPercent, value); }

		}

		  
		[XmlAttribute("Description")]
		public string Description 
		{
			get { return GetColumnValue<string>(Columns.Description); }

			set { SetColumnValue(Columns.Description, value); }

		}

		
		
		//private DateTime? propCreatedOn;
		[XmlAttribute("CreatedOn")]
		public DateTime CreatedOn 
		{
			get
			{
			DateTime? propCreatedOn = GetColumnValue<DateTime?>(Columns.CreatedOn);
				if (!propCreatedOn.HasValue)
					return new DateTime(1900,01, 01);
				return propCreatedOn.Value;
			}

		   set { SetColumnValue(Columns.CreatedOn, value); }

		}

		
		[XmlIgnore]
		public bool CreatedOnHasValue
		{
			get { return GetColumnValue<DateTime?>("createdOn") != null; }

			set
			{
				DateTime? propCreatedOn = GetColumnValue<DateTime?>(Columns.CreatedOn);
				if (!value)
					SetColumnValue(Columns.CreatedOn, null);
				else if (value && !propCreatedOn.HasValue)
					SetColumnValue(Columns.CreatedOn, new DateTime(1900,01, 01));
		   }

		}

		
		  
		[XmlAttribute("CreatedBy")]
		public string CreatedBy 
		{
			get { return GetColumnValue<string>(Columns.CreatedBy); }

			set { SetColumnValue(Columns.CreatedBy, value); }

		}

		
		
		//private DateTime? propModifiedOn;
		[XmlAttribute("ModifiedOn")]
		public DateTime ModifiedOn 
		{
			get
			{
			DateTime? propModifiedOn = GetColumnValue<DateTime?>(Columns.ModifiedOn);
				if (!propModifiedOn.HasValue)
					return new DateTime(1900,01, 01);
				return propModifiedOn.Value;
			}

		   set { SetColumnValue(Columns.ModifiedOn, value); }

		}

		
		[XmlIgnore]
		public bool ModifiedOnHasValue
		{
			get { return GetColumnValue<DateTime?>("modifiedOn") != null; }

			set
			{
				DateTime? propModifiedOn = GetColumnValue<DateTime?>(Columns.ModifiedOn);
				if (!value)
					SetColumnValue(Columns.ModifiedOn, null);
				else if (value && !propModifiedOn.HasValue)
					SetColumnValue(Columns.ModifiedOn, new DateTime(1900,01, 01));
		   }

		}

		
		  
		[XmlAttribute("ModifiedBy")]
		public string ModifiedBy 
		{
			get { return GetColumnValue<string>(Columns.ModifiedBy); }

			set { SetColumnValue(Columns.ModifiedBy, value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Commerce.Common.PromoProductBundleMapCollection PromoProductBundleMapRecords()
		{
			return new Commerce.Common.PromoProductBundleMapCollection().Where(PromoProductBundleMap.Columns.BundleID, BundleID).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		#region Many To Many Helpers
		
		 
		public Commerce.Common.ProductCollection GetProductCollection() { return PromoBundle.GetProductCollection(this.BundleID); }

		public static Commerce.Common.ProductCollection GetProductCollection(int varBundleID)
		{
		    SubSonic.QueryCommand cmd = new SubSonic.QueryCommand("SELECT * FROM [dbo].[CSK_Store_Product] INNER JOIN [CSK_Promo_Product_Bundle_Map] ON [CSK_Store_Product].[productID] = [CSK_Promo_Product_Bundle_Map].[productID] WHERE [CSK_Promo_Product_Bundle_Map].[bundleID] = @bundleID", PromoBundle.Schema.Provider.Name);
			cmd.AddParameter("@bundleID", varBundleID, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			ProductCollection coll = new ProductCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveProductMap(int varBundleID, ProductCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM [CSK_Promo_Product_Bundle_Map] WHERE [CSK_Promo_Product_Bundle_Map].[bundleID] = @bundleID", PromoBundle.Schema.Provider.Name);
			cmdDel.AddParameter("@bundleID", varBundleID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Product item in items)
			{
				PromoProductBundleMap varPromoProductBundleMap = new PromoProductBundleMap();
				varPromoProductBundleMap.SetColumnValue("bundleID", varBundleID);
				varPromoProductBundleMap.SetColumnValue("productID", item.GetPrimaryKeyValue());
				varPromoProductBundleMap.Save();
			}

		}

		public static void SaveProductMap(int varBundleID, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM [CSK_Promo_Product_Bundle_Map] WHERE [CSK_Promo_Product_Bundle_Map].[bundleID] = @bundleID", PromoBundle.Schema.Provider.Name);
			cmdDel.AddParameter("@bundleID", varBundleID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					PromoProductBundleMap varPromoProductBundleMap = new PromoProductBundleMap();
					varPromoProductBundleMap.SetColumnValue("bundleID", varBundleID);
					varPromoProductBundleMap.SetColumnValue("productID", l.Value);
					varPromoProductBundleMap.Save();
				}

			}

		}

		public static void SaveProductMap(int varBundleID , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM [CSK_Promo_Product_Bundle_Map] WHERE [CSK_Promo_Product_Bundle_Map].[bundleID] = @bundleID", PromoBundle.Schema.Provider.Name);
			cmdDel.AddParameter("@bundleID", varBundleID);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				PromoProductBundleMap varPromoProductBundleMap = new PromoProductBundleMap();
				varPromoProductBundleMap.SetColumnValue("bundleID", varBundleID);
				varPromoProductBundleMap.SetColumnValue("productID", item);
				varPromoProductBundleMap.Save();
			}

		}

		
		public static void DeleteProductMap(int varBundleID) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM [CSK_Promo_Product_Bundle_Map] WHERE [CSK_Promo_Product_Bundle_Map].[bundleID] = @bundleID", PromoBundle.Schema.Provider.Name);
			cmdDel.AddParameter("@bundleID", varBundleID);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varBundleName,int varDiscountPercent,string varDescription,DateTime varCreatedOn,string varCreatedBy,DateTime varModifiedOn,string varModifiedBy)
		{
			PromoBundle item = new PromoBundle();
			
			item.BundleName = varBundleName;
			
			item.DiscountPercent = varDiscountPercent;
			
			item.Description = varDescription;
			
			item.CreatedOn = varCreatedOn;
			
			item.CreatedBy = varCreatedBy;
			
			item.ModifiedOn = varModifiedOn;
			
			item.ModifiedBy = varModifiedBy;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varBundleID,string varBundleName,int varDiscountPercent,string varDescription,DateTime varCreatedOn,string varCreatedBy,DateTime varModifiedOn,string varModifiedBy)
		{
			PromoBundle item = new PromoBundle();
			
				item.BundleID = varBundleID;
			
				item.BundleName = varBundleName;
			
				item.DiscountPercent = varDiscountPercent;
			
				item.Description = varDescription;
			
				item.CreatedOn = varCreatedOn;
			
				item.CreatedBy = varCreatedBy;
			
				item.ModifiedOn = varModifiedOn;
			
				item.ModifiedBy = varModifiedBy;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn BundleIDColumn
        {
            get { return Schema.Columns[0]; }

        }

        
        
        
        public static TableSchema.TableColumn BundleNameColumn
        {
            get { return Schema.Columns[1]; }

        }

        
        
        
        public static TableSchema.TableColumn DiscountPercentColumn
        {
            get { return Schema.Columns[2]; }

        }

        
        
        
        public static TableSchema.TableColumn DescriptionColumn
        {
            get { return Schema.Columns[3]; }

        }

        
        
        
        public static TableSchema.TableColumn CreatedOnColumn
        {
            get { return Schema.Columns[4]; }

        }

        
        
        
        public static TableSchema.TableColumn CreatedByColumn
        {
            get { return Schema.Columns[5]; }

        }

        
        
        
        public static TableSchema.TableColumn ModifiedOnColumn
        {
            get { return Schema.Columns[6]; }

        }

        
        
        
        public static TableSchema.TableColumn ModifiedByColumn
        {
            get { return Schema.Columns[7]; }

        }

        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string BundleID = @"bundleID";
			 public static string BundleName = @"bundleName";
			 public static string DiscountPercent = @"discountPercent";
			 public static string Description = @"description";
			 public static string CreatedOn = @"createdOn";
			 public static string CreatedBy = @"createdBy";
			 public static string ModifiedOn = @"modifiedOn";
			 public static string ModifiedBy = @"modifiedBy";
						
		}

		#endregion
		
		
		
	}

}

